package com.knowledgegraph.controller;

import com.knowledgegraph.entity.NodeResource;
import com.knowledgegraph.entity.Resource;
import com.knowledgegraph.service.KnowledgeGraphService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/knowledge-graph")
@Tag(name = "Resource API", description = "知识图谱资源管理接口")
public class ResourceController {
    
    @Autowired
    private KnowledgeGraphService knowledgeGraphService;
    
    @PostMapping("/resources")
    @Operation(summary = "创建资源", description = "创建新的资源")
    public ResponseEntity<Resource> createResource(@Valid @RequestBody Resource resource) {
        Resource createdResource = knowledgeGraphService.createResource(resource);
        return ResponseEntity.ok(createdResource);
    }
    
    @GetMapping("/resources/{id}")
    @Operation(summary = "获取资源详情", description = "根据ID获取资源详细信息")
    public ResponseEntity<Resource> getResource(@Parameter(description = "资源ID") @PathVariable Long id) {
        Optional<Resource> resource = knowledgeGraphService.getResourceById(id);
        return resource.map(ResponseEntity::ok)
                      .orElse(ResponseEntity.notFound().build());
    }
    
    @GetMapping("/resources")
    @Operation(summary = "获取所有资源", description = "获取所有资源列表")
    public ResponseEntity<List<Resource>> getAllResources(
            @Parameter(description = "资源类型过滤") @RequestParam(required = false) String resourceType,
            @Parameter(description = "名称搜索") @RequestParam(required = false) String name) {
        
        List<Resource> resources;
        if (resourceType != null && !resourceType.isEmpty()) {
            resources = knowledgeGraphService.getResourcesByType(resourceType);
        } else if (name != null && !name.isEmpty()) {
            resources = knowledgeGraphService.searchResourcesByName(name);
        } else {
            resources = knowledgeGraphService.getAllResources();
        }
        
        return ResponseEntity.ok(resources);
    }
    
    @PutMapping("/resources/{id}")
    @Operation(summary = "更新资源", description = "更新现有资源信息")
    public ResponseEntity<Resource> updateResource(
            @Parameter(description = "资源ID") @PathVariable Long id,
            @Valid @RequestBody Resource resource) {
        
        if (!knowledgeGraphService.getResourceById(id).isPresent()) {
            return ResponseEntity.notFound().build();
        }
        
        resource.setId(id);
        Resource updatedResource = knowledgeGraphService.updateResource(resource);
        return ResponseEntity.ok(updatedResource);
    }
    
    @DeleteMapping("/resources/{id}")
    @Operation(summary = "删除资源", description = "删除指定资源")
    public ResponseEntity<Void> deleteResource(@Parameter(description = "资源ID") @PathVariable Long id) {
        if (!knowledgeGraphService.getResourceById(id).isPresent()) {
            return ResponseEntity.notFound().build();
        }
        
        knowledgeGraphService.deleteResource(id);
        return ResponseEntity.noContent().build();
    }
    
    @PostMapping("/node-resources")
    @Operation(summary = "创建节点资源关联", description = "创建节点与资源的关联关系")
    public ResponseEntity<NodeResource> createNodeResourceRelation(@Valid @RequestBody NodeResource nodeResource) {
        NodeResource createdRelation = knowledgeGraphService.createNodeResourceRelation(nodeResource);
        return ResponseEntity.ok(createdRelation);
    }
    
    @GetMapping("/nodes/{nodeId}/resources")
    @Operation(summary = "获取节点关联资源", description = "获取指定节点关联的所有资源")
    public ResponseEntity<List<Resource>> getResourcesByNode(@Parameter(description = "节点ID") @PathVariable Long nodeId) {
        List<Resource> resources = knowledgeGraphService.getResourcesByNode(nodeId);
        return ResponseEntity.ok(resources);
    }
    
    @GetMapping("/resources/{resourceId}/nodes")
    @Operation(summary = "获取资源关联节点", description = "获取与指定资源关联的所有节点")
    public ResponseEntity<List<com.knowledgegraph.entity.Node>> getNodesByResource(
            @Parameter(description = "资源ID") @PathVariable Long resourceId) {
        List<com.knowledgegraph.entity.Node> nodes = knowledgeGraphService.getNodesByResource(resourceId);
        return ResponseEntity.ok(nodes);
    }
    
    @DeleteMapping("/node-resources/{id}")
    @Operation(summary = "删除节点资源关联", description = "删除指定的节点资源关联关系")
    public ResponseEntity<Void> deleteNodeResourceRelation(@Parameter(description = "关联关系ID") @PathVariable Long id) {
        knowledgeGraphService.deleteNodeResourceRelation(id);
        return ResponseEntity.noContent().build();
    }
}